package daoImp;

import Util.FileIOUtil;
import dao.PetDao;
import entity.Pet;
import entity.PetType;

import java.util.ArrayList;

/**
 * @Author: Tiangle
 * @Date: 2021/9/13 9:33
 */
public class PetDaoImp implements PetDao {
    FileIOUtil fio = new FileIOUtil();
    String petsFile = "pets.txt";
    String petType = "petType.txt";
    String auditPet = "petAudit.txt";
    @Override
    public boolean addPet(Pet pet) {
        ArrayList<Pet> al = getPets();
        boolean isOk = al.add(pet);
        boolean isOk2 = fio.output(petsFile,al);
        return isOk&&isOk2;
    }

    @Override
    public Pet getPet(int id) {
        ArrayList<Pet> al = getPets();
        if (al.size()==0)
            return null;
        for (Pet m:al
        ) {
            if (m.getId()==id){
                return m;
            }
        }
        return null;
    }

    @Override
    public boolean editPet(Pet pet) {
        if (getPet(pet.getId())==null)
            return false;
        ArrayList<Pet> al = getPets();
        for (int i = 0; i < al.size(); i++) {
            if (al.get(i).getId()==pet.getId()){
                al.get(i).upData(pet);
                fio.output(petsFile,al);
                return true;
            }
        }
        return false;
    }

    @Override
    public ArrayList<Pet> getPets() {
        ArrayList<Pet> al;
        al = fio.input(petsFile);
        return al;
    }

    @Override
    public int addPetType(String breed) {
        ArrayList<PetType> al= fio.input(petType);
        PetType pt = new PetType(getTypeId(),breed);
        al.add(pt);
        fio.output(petType,al);
        return pt.getId();
    }

    @Override
    public ArrayList<PetType> getPetType() {
        ArrayList<PetType> al;
        al = fio.input(petType);
        return al;
    }

    @Override
    public boolean deletePet(int id) {
        ArrayList<Pet> al = getPets();
        if (al.size()==0)
            return false;
        for (Pet p:al
             ) {
            if (p.getId()==id){
                al.remove(p);
                fio.output(petsFile,al);
                return true;
            }
        }
        return false;
    }

    @Override
    public int getTypeId() {
        ArrayList<PetType> al= fio.input(petType);
        int x = al.size()+1;
        boolean flag = false;
        for (int i = 1; i <= x; i++) {
            flag = false;
            for (PetType e:al
            ) {
                if (e.getId()==i){
                    flag = true;
                }
            }
            if (!flag){
                x = i;
                break;
            }
        }
        return x;
    }

    @Override
    public int getPetId() {
        ArrayList<Pet> al= getPets();
        ArrayList<Pet> al2 = getAuditPets();
        al.addAll(al2);
        int x = al.size()+1;
        boolean flag = false;
        for (int i = 1; i <= x; i++) {
            flag = false;
            for (Pet e:al
            ) {
                if (e.getId()==i){
                    flag = true;
                }
            }
            if (!flag){
                x = i;
                break;
            }
        }
        return x;
    }

    @Override
    public boolean addauditPet(Pet pet) {
        ArrayList<Pet> al = getAuditPets();
        boolean isOk = al.add(pet);
        boolean isOk2 = fio.output(auditPet,al);
        return isOk&&isOk2;
    }

    @Override
    public boolean deleteAuditPet(int id) {
        ArrayList<Pet> al = getAuditPets();
        if (al.size()==0)
            return false;
        for (Pet p:al
        ) {
            if (p.getId()==id){
                al.remove(p);
                fio.output(auditPet,al);
                return true;
            }
        }
        return false;
    }

    @Override
    public ArrayList<Pet> getAuditPets() {
        ArrayList<Pet> al;
        al = fio.input(auditPet);
        return al;
    }

    @Override
    public Pet getAuditPet(int id) {
        ArrayList<Pet> auditPets = getAuditPets();
        for (Pet p :
                auditPets) {
            if (p.getId() == id){
                return p;
            }
        }
        return null;
    }
}
